<%@page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/vendor/bootstrap/css/bootstrap.css">
    <!-- Font Awesome CSS-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/vendor/font-awesome/css/font-awesome.min.css">
    <!-- Custom Font Icons CSS-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/font.css">
    <!-- theme stylesheet-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.default.css" id="theme-stylesheet">
    <!-- Custom stylesheet - for your changes-->
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/custom.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/layer.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap/css/bootstrap-switch.min.css">
    <title>查看用户</title>
</head>
<body>
<%@include file="../publicjsp/head.jsp" %>

<div class="d-flex align-items-stretch">
    <!-- Sidebar Navigation-->
    <nav id="sidebar">
        <!-- Sidebar Header-->
        <%@ include file="../publicjsp/sidhead.jsp" %>
        <!-- Sidebar Navidation Menus--><span class="heading">Main</span>
        <%@ include file="../publicjsp/sidenav.jsp" %>

    </nav>
    <!-- Sidebar Navigation end-->
    <div class="page-content">
        <div class="page-header">
            <div class="container-fluid">
                <h2 class="h5 no-margin-bottom">用户列表</h2>
            </div>
        </div>
        <section class="no-padding-bottom">
            <div class="title">
                <form class="form-inline" method="get" action="${pageContext.request.contextPath}/user/getUser">
                    <div class="form-group">
                        <label for="name" class="sr-only">Name</label>
                        <%--<c:if test="${name == null}">--%>
                            <%--<input id="name" name="name" type="text" placeholder="按名字查找"--%>
                                   <%--class="mr-sm-3 form-control">--%>
                        <%--</c:if>--%>
                        <%--<c:if test="${name != null}">--%>
                            <input id="name" value="${name}" name="name" type="text" placeholder="按名字查找"
                                   class="mr-sm-3 form-control">
                        <%--</c:if>--%>
                    </div>
                    <div class="form-group">
                        <input type="submit" value="查询" class="btn btn-primary">
                    </div>
                </form>

            </div>
            <div class="table-responsive">
                <table class="table">
                    <thead>
                    <tr>
                        <th>ID</th>
                        <th>姓名</th>
                        <th>性别</th>
                        <th>年龄</th>
                        <th>简介</th>
                        <th>操作</th>
                        <th>加关注</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${userList}" var="user">
                        <tr>
                            <th scope="row">${user.user_id}</th>
                            <td>${user.real_name}</td>
                            <td>
                                <c:if test="${user.gender==1}">
                                    男
                                </c:if>
                                <c:if test="${user.gender==0}">
                                    女
                                </c:if>
                            </td>
                            <td>${user.age}</td>
                            <td>${user.introduction}</td>
                            <td>
                                <input type="submit" value="详细信息" class="btn-xs btn-primary userDetail">
                            </td>

                            <td>
                                <c:if test="${user.fid == null}">
                                    <input onclick="followUser(this,'${user.user_id}')" type="checkbox" value=""
                                           class="checkbox-template">
                                </c:if>

                                <c:if test="${user.fid != null}">
                                    <input onclick="followUser(this,'${user.user_id}')" type="checkbox" value=""
                                           class="checkbox-template" checked>
                                </c:if>
                            </td>
                        </tr>
                    </c:forEach>

                    </tbody>
                </table>

                <nav class="text-center" aria-label="Page navigation">
                    <ul class="pagination">
                        <li>
                            <a href="JavaScript:;" id="pre">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>

                        <c:forEach varStatus="vs" begin="${startPage}" end="${endPage}" step="1">
                            <c:if test="${vs.index == currentPage}">
                                <li><a class="bor one" href="JavaScript:;">${vs.index}</a></li>
                            </c:if>

                            <c:if test="${vs.index != currentPage}">
                                <li><a class="one" id="two" href="JavaScript:;">${vs.index}</a></li>
                            </c:if>
                        </c:forEach>
                        <li>
                            <a href="JavaScript:;" id="next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </section>

        <%@include file="../publicjsp/footer.jsp" %>
    </div>
</div>

<!-- JavaScript files-->
<script src="${pageContext.request.contextPath}/vendor/jquery/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/vendor/popper.js/umd/popper.min.js"></script>
<script src="${pageContext.request.contextPath}/vendor/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap-switch.min.js"></script>
<script src="${pageContext.request.contextPath}/vendor/jquery.cookie/jquery.cookie.js"></script>
<script src="${pageContext.request.contextPath}/vendor/chart.js/Chart.min.js"></script>
<script src="${pageContext.request.contextPath}/js/layer.js"></script>
<script src="${pageContext.request.contextPath}/vendor/jquery-validation/jquery.validate.min.js"></script>
<script src="${pageContext.request.contextPath}/js/charts-home.js"></script>
<script src="${pageContext.request.contextPath}/js/front.js"></script>
<script src="${pageContext.request.contextPath}/js/custom.js"></script>


<script>


    $(function () {
       $(".one").on("click",function () {
           let page = $(this).html();
           let name = $("#name").val();
           console.log(page);
           console.log(name);
           if (name === '' || null == name) {
               window.location.href = '${pageContext.request.contextPath}/user/getUser?page='+page;
           } else {
               window.location.href = "${pageContext.request.contextPath}/user/getUser?page="+ page+ "&name=" + name;
           }
       });
    });

    function cPage(page) {

    }

    let next = document.getElementById("next");
    next.onclick = function () {
        let page = parseInt('${startPage}') + parseInt('${pageCount}');
        let pageTotal = parseInt('${pageTotal}');
        if (page > pageTotal) {
            layer.msg("已经到底页了");
            return
        }
        let name = "${name}";
        if ('' === name || null == name) {
            window.location.href = '${pageContext.request.contextPath}/user/getUser?page=' + page;
        } else {
            window.location.href = '${pageContext.request.contextPath}/user/getUser?page=' + page + "&name=" + name;
        }
    };

    let pre = document.getElementById("pre");
    pre.onclick = function () {
        let page = parseInt('${startPage}') - 1;
        if (page === 0) {
            layer.msg("这已经是顶页了");
            return;
        }
        let name = "${name}";
        if (name === '' || null == name) {
            window.location.href = '${pageContext.request.contextPath}/user/getUser?page=' + page;
        } else {
            window.location.href = '${pageContext.request.contextPath}/user/getUser?page=' + page + "&name=" + name;
        }
    };

    function followUser(obj, ufId) {
        let tag = $(obj).prop("checked");
        if (tag) {
            $.post('${pageContext.request.contextPath}/user/followUser', {ufId: ufId}, function (data) {
                if (data === "1" ) {
                    layer.msg("关注成功");
                } else {
                    layer.msg(data);
                    $(obj).prop("checked",false);
                }
            })
        } else {
            $.post('${pageContext.request.contextPath}/user/unfollowUser',{ufId:ufId},function (data) {
                console.log(data);
                if (data === "1"){
                    layer.msg("取关成功");
                } else {
                    layer.msg(data);
                    $(obj).prop("checked",true);
                }
            })
        }

    }


</script>
</body>
</html>